/**
 * @(#)AS400Column.java	0.1 Nov 6, 2007
 *
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.wideskill.rpg4ejb3.type.annotation;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.util.Arrays;

import com.wideskill.rpg4ejb3.type.BrandType;

/**
 * @author: Luigi Scarpato <luigi.scarpato at wideskill.com>
 * 
 */
@Documented
@BrandType(As400ColumnType.class)
@Target( { FIELD })
@Retention(RUNTIME)
public @interface AS400Column {
	public enum Usage {
		INPUT, OUTPUT
	}

	enum Justified {
		LEFT, RIGHT;
	};
	
	public enum Fill {
		LEFT_ZERO {
			public Object valorize(Object s, int maxLen) {
				if (s == null) return null;

				return fixedLength((String)s.toString(), '0',  Justified.RIGHT, maxLen);
			}
		},
		RIGHT_ZERO {
			public Object valorize(Object s, int maxLen) {
				if (s == null) return null;
				
				return fixedLength((String)s.toString(), '0',  Justified.LEFT, maxLen);
			}
		},
		NONE {
			public Object valorize(Object s, int maxLen) {
				return s;
			}
		};

		abstract public Object valorize(Object s, int maxLen);

		private static String fixedLength(String s, Character c, Justified justified, int length) {
			char output[] = new char[length];
			Arrays.fill(output, c);
			System.arraycopy(s.toCharArray(), 0, output,  justified == Justified.RIGHT ? length - s.length() : 0, s.length());
			return new String(output);	
		};
	};

	String name();

	Usage usage() default Usage.INPUT;

	Fill normalize() default Fill.NONE;
}
